package com.huawei.appmarket.service.externalapi.actions;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import androidx.annotation.Nullable;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.installdepend.bean.GetDetailByIdResBean;
import com.huawei.appmarket.service.installdepend.bean.GetDetailByIdResBean$DetailInfoBean;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import o.bcj;
import o.bcm;
import o.bcn;
import o.ccz;
import o.cdb;
import o.cdg;
import o.cfd;
import o.cfe;
import o.cfg;
import o.cfw;
import o.BaseRequest;
import o.Logger;
import o.ContentManager;
import o.dyr;
import o.eaj;
import o.CollectCheck;
import o.fho;

public class GuideCommentAction extends dyr {
    private static final int LEGAL_APP = 1;
    private static final String TAG = "GuideCommentAction";
    private bcj mCommentDiag;
    private String mPackageName = "";

    class a implements cfd {
        final /* synthetic */ GuideCommentAction ˋ;

        private a(GuideCommentAction guideCommentAction) {
            this.ˋ = guideCommentAction;
        }

        public void ˋ(int i, int i2, @Nullable List<cfe> list) {
            if (CollectCheck.isEmpty(list)) {
                Logger.ˋ(GuideCommentAction.TAG, "Check validate from server ret null");
                this.ˋ.setRetCodeAndExit(DialogButton.CONFIRM);
            } else if (((cfe) list.get(0)).ˏ == 1) {
                cfw.ˎ(new eaj(this.ˋ.mPackageName), new c());
            } else {
                Logger.ˋ(GuideCommentAction.TAG, "App is not validate");
                this.ˋ.setRetCodeAndExit(DialogButton.CONFIRM);
            }
        }
    }

    class c implements IServerCallBack {
        final /* synthetic */ GuideCommentAction ॱ;

        private c(GuideCommentAction guideCommentAction) {
            this.ॱ = guideCommentAction;
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            if (responseBean instanceof GetDetailByIdResBean) {
                List ˏ = ((GetDetailByIdResBean) responseBean).ˏ();
                if (CollectCheck.isEmpty(ˏ)) {
                    this.ॱ.setRetCodeAndExit(DialogButton.CONFIRM);
                    return;
                }
                final PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(this.ॱ.mPackageName);
                if (ᐝ == null || ᐝ.versionName == null) {
                    this.ॱ.setRetCodeAndExit(DialogButton.CONFIRM);
                    return;
                }
                final GetDetailByIdResBean$DetailInfoBean getDetailByIdResBean$DetailInfoBean = (GetDetailByIdResBean$DetailInfoBean) ˏ.get(0);
                ((bcm) fho.ˏ().ˋ("AppComment").ॱ(bcm.class)).ॱ(getDetailByIdResBean$DetailInfoBean.ॱˊ(), ᐝ.versionName, 1, ccz.ॱ(), new bcn(this) {
                    final /* synthetic */ c ˎ;

                    public void ˊ(String str, String str2, String str3) {
                        o.bch.a aVar = new o.bch.a();
                        aVar.ॱॱ(str3);
                        aVar.ʼ(str);
                        aVar.ʻ(str2);
                        aVar.ˎ(getDetailByIdResBean$DetailInfoBean.ᐝ());
                        aVar.ˋ(getDetailByIdResBean$DetailInfoBean.ॱˊ());
                        aVar.ˏ(getDetailByIdResBean$DetailInfoBean.ˊॱ());
                        aVar.ॱ(getDetailByIdResBean$DetailInfoBean.ʻ());
                        aVar.ˊ(getDetailByIdResBean$DetailInfoBean.ˏॱ());
                        aVar.ᐝ(ᐝ.versionName);
                        this.ˎ.ॱ.mCommentDiag = (bcj) fho.ˏ().ˋ("AppComment").ॱ(bcj.class);
                        this.ˎ.ॱ.mCommentDiag.ˋ(aVar.ˋ(), this.ˎ.ॱ.callback);
                    }
                });
            }
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }
    }

    public GuideCommentAction(o.dys.a aVar) {
        super(aVar);
    }

    public void onAction() {
        this.mPackageName = this.callback.ˏ();
        checkAppValidateAndLauncherDialog(this.mPackageName);
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        setRetCodeAndExit(i2);
    }

    private boolean checkAppValidateAndLauncherDialog(String str) {
        PackageManager packageManager = ContentManager.getInstant().getContext().getPackageManager();
        if (packageManager == null) {
            return false;
        }
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(str, 1);
            List arrayList = new ArrayList();
            List arrayList2 = new ArrayList();
            arrayList.add(packageInfo.packageName);
            arrayList2.add(getSHA256(packageInfo.applicationInfo.sourceDir));
            ((cfg) cdb.ॱ(cfg.class)).checkAppValidate(arrayList, arrayList2, new a());
            return true;
        } catch (NameNotFoundException e) {
            Logger.ˋ(TAG, "no package: " + str);
            return false;
        }
    }

    private static String getSHA256(String str) {
        File file = new File(str);
        if (file.exists()) {
            byte[] manifestFile = getManifestFile(file);
            StringBuilder stringBuilder = new StringBuilder();
            try {
                MessageDigest instance = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256);
                instance.update(manifestFile);
                for (byte b : instance.digest()) {
                    String toHexString = Integer.toHexString(b & 255);
                    if (toHexString.length() < 2) {
                        stringBuilder.append("0");
                    }
                    stringBuilder.append(toHexString);
                }
                return stringBuilder.toString();
            } catch (NoSuchAlgorithmException e) {
                Logger.ˋ(TAG, "Get sha256 failed: NoSuchAlgorithmException");
                return "";
            }
        }
        Logger.ˋ(TAG, "Get sha256 failed. Apk not exists!");
        return null;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static byte[] getManifestFile(java.io.File r8) {
        /*
        r0 = 0;
        r6 = 0;
        r1 = new java.io.ByteArrayOutputStream;
        r1.<init>();
        r2 = 1024; // 0x400 float:1.435E-42 double:5.06E-321;
        r3 = new byte[r2];
        r2 = new java.util.zip.ZipFile;	 Catch:{ IOException -> 0x006a, all -> 0x004e }
        r2.<init>(r8);	 Catch:{ IOException -> 0x006a, all -> 0x004e }
        r4 = "META-INF/MANIFEST.MF";
        r4 = r2.getEntry(r4);	 Catch:{ IOException -> 0x0029, all -> 0x005a }
        if (r4 == 0) goto L_0x0047;
    L_0x0018:
        r0 = r2.getInputStream(r4);	 Catch:{ IOException -> 0x0029, all -> 0x005a }
        if (r0 == 0) goto L_0x0047;
    L_0x001e:
        r4 = r0.read(r3);	 Catch:{ IOException -> 0x0029, all -> 0x005f }
        if (r4 <= 0) goto L_0x003b;
    L_0x0024:
        r5 = 0;
        r1.write(r3, r5, r4);	 Catch:{ IOException -> 0x0029, all -> 0x005f }
        goto L_0x001e;
    L_0x0029:
        r1 = move-exception;
        r1 = r2;
    L_0x002b:
        r2 = "GuideCommentAction";
        r3 = "get manifest file failed due to IOException";
        o.dna.ˋ(r2, r3);	 Catch:{ all -> 0x0064 }
        o.dnw.ˎ(r0);
        o.dnw.ˎ(r1);
    L_0x0038:
        r0 = new byte[r6];
    L_0x003a:
        return r0;
    L_0x003b:
        r1 = r1.toByteArray();	 Catch:{ IOException -> 0x0029, all -> 0x005f }
        o.dnw.ˎ(r0);
        o.dnw.ˎ(r2);
        r0 = r1;
        goto L_0x003a;
    L_0x0047:
        o.dnw.ˎ(r0);
        o.dnw.ˎ(r2);
        goto L_0x0038;
    L_0x004e:
        r1 = move-exception;
        r2 = r0;
        r7 = r0;
        r0 = r1;
        r1 = r7;
    L_0x0053:
        o.dnw.ˎ(r1);
        o.dnw.ˎ(r2);
        throw r0;
    L_0x005a:
        r1 = move-exception;
        r7 = r1;
        r1 = r0;
        r0 = r7;
        goto L_0x0053;
    L_0x005f:
        r1 = move-exception;
        r7 = r1;
        r1 = r0;
        r0 = r7;
        goto L_0x0053;
    L_0x0064:
        r2 = move-exception;
        r7 = r2;
        r2 = r1;
        r1 = r0;
        r0 = r7;
        goto L_0x0053;
    L_0x006a:
        r1 = move-exception;
        r1 = r0;
        goto L_0x002b;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.appmarket.service.externalapi.actions.GuideCommentAction.getManifestFile(java.io.File):byte[]");
    }

    private void setRetCodeAndExit(int i) {
        this.callback.setResult(i);
        this.callback.finish();
    }
}
